<?php namespace Common\Model;
use Hdphp\Model\Model;

class Commod extends Model{
    protected $table='commodity';

    protected $validate=array(
        array('coname','maxlen:45','商品名称最大不能超过45个字符',3,3),
    );

    protected $auto=array(
        array('cotime','time','function',3,1)
    );
//    商品的添加
    public function store(){
        if(!$this->check()) return false;
        $coid=$this->add();
//        p(Q('post.'));die;
//        添加商品属性
        $coattributeData=new \Common\Model\Coattribute();
//        属性添加
//        p(Q('post.attr'));die;
        foreach(Q('post.attr')as $taid=>$v){
            if($v){
                $data=array(
                    'coattvalue'=>$v,
                    'shop_typeattribute_taid'=>$taid,
                    'shop_commodity_coid'=>$coid,
                );
                $coattributeData->add($data);
            }
        }
//        添加商品规格
        $spec=Q('post.spec');
        if($spec){
            $value=$spec['value'];
            foreach($value as $taid=>$v){
                foreach($v as $kk=>$vv){
                    $data=array(
                        'coattvalue'=>$vv,
                        'shop_typeattribute_taid'=>$taid,
                        'shop_commodity_coid'=>$coid,
                        'coattaddpri'=>$spec['added'][$taid][$kk]
                    );
                    $coattributeData->add($data);
                }
            }
        }

//        添加商品详细信息
        $commodetailModel=new \Common\Model\Commoddetil();
        $commodetailModel->create();
        $coimg=implode('|',Q('post.coimg'));
        $commodetailModel->data['shop_commodity_coid']=$coid;
        $commodetailModel->data['coimg']=$coimg;
        $commodetailModel->add();

        return true;
    }
//    编辑
    public function edit(){
//       一，先验证
        if(!$this->check()) return false;
//        p(Q('post.colistimg'));die;
        if(!Q('post.colistimg')){$this->data['colistimg']='';}
        $this->save();
        $coid=Q('post.shop_commodity_coid',0,'intval');
        $commodlistModel=new \Common\Model\Commodlist();
        $commodlistModel->where("shop_commodity_coid={$coid}")->delete();
        $coattrbuteData=new \Common\Model\Coattribute;
        $coattrbuteData->where("shop_commodity_coid",$coid)->delete();
//        属性添加
//        p(Q('post.attr'));
        foreach(Q('post.attr')as $taid=>$v){
            if($v){
                $data=array(
                    'coattvalue'=>$v,
                    'shop_typeattribute_taid'=>$taid,
                    'shop_commodity_coid'=>$coid,
                );
                $coattrbuteData->add($data);
            }
        }
//        添加商品规格
        $spec=Q('post.spec');
//        p($spec);die;
        if($spec){
            $value=$spec['value'];
            foreach($value as $taid=>$v){
                foreach($v as $kk=>$vv){
                    $data=array(
                        'coattvalue'=>$vv,
                        'shop_typeattribute_taid'=>$taid,
                        'shop_commodity_coid'=>$coid,
                        'coattaddpri'=>$spec['added'][$taid][$kk]
                    );
                    $coattrbuteData->add($data);
                }
            }
        }
//        p($coattrbuteData->get());die;
//        $coattrbuteData->add();

        $coattrbuteData->where("shop_commodity_coid={$coid}")->save();
        $codetailData=new \Common\Model\Commoddetil();
        if(!Q('post.coimg')){$codetailData->data['coimg']='';}
        else{
            $coimg=implode('|',Q('post.coimg'));$codetailData->data['coimg']=$coimg;
        }
//        p($a);die;
//        $codetailData->create();
        $codetailData->where("shop_commodity_coid={$coid}")->save();
        return true;
    }


    //    因为验证两张表，所以把验证方法单独写入一个方法
    private function check(){
        //create方法：让$this->data有数据，触发自动验证自动完成
        if(!$this->create())return false;
//            如果有错误信息，上传失败
        if($this->error) return false;
//            验证商品详细表
        $codetailData=new \Common\Model\Commoddetil();
        if(!$codetailData->create()){
            $this->error=$codetailData->getError();
            return false;
        }
        return true;
    }
}